package com.leo.springbootmultithreadbasic.buildthread.extendsthread;

import java.util.Random;

/**
 * <p>该Demo体现线程调用的随机性</p>
 *
 * 实现思路：
 * 1.在主线程里执行一个for循环
 * 2.在子线程里执行一个for循环
 *
 *
 * @author Leo
 * @version V1.0
 * @date 2021/10/30 8:50 PM
 */
public class Run2 {
    public static void main(String[] args) throws InterruptedException {

        // 调用子线程
        MyThread2 subThread = new MyThread2();
        subThread.setName("SubThread");
        subThread.start();

        // 主线程中的测试方法
        testMain();

    }

    private static void testMain() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(new Random().nextInt(1000));
            System.out.println("main=" + Thread.currentThread().getName());
        }
    }


}

class MyThread2 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 10 ; i++) {
            try {
                Thread.sleep(new Random().nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("run=" + Thread.currentThread().getName());
        }
    }
}
